package com.newlink.building.userinfo.adapter

import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.newlink.building.userinfo.R

/**
 * Created by xiaodong on 2024/11/5.
 */
class Module_UserInfo_ImageAdapter(private val imageUriList: List<Uri>) :
    RecyclerView.Adapter<Module_UserInfo_ImageAdapter.ImageViewHolder?>() {

    private var onItemClickListener: OnItemClickListener? = null
    fun setOnItemClickListener(onItemClickListener: OnItemClickListener) {
        this.onItemClickListener = onItemClickListener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ImageViewHolder {
        val view: View =
            LayoutInflater.from(parent.context).inflate(R.layout.item_image, parent, false)
        return ImageViewHolder(view)
    }

    override fun onBindViewHolder(holder: ImageViewHolder, position: Int) {
        val imageUri = imageUriList[position]
        Glide.with(holder.imageView.context).load(imageUri).into(holder.imageView)
        holder.imageView.setOnClickListener { v ->
            onItemClickListener?.itemClick(v, position)
        }
        holder.delete.setOnClickListener { v ->
            onItemClickListener?.deleteClick(v, position)
        }
    }

    override fun getItemCount(): Int {
        return imageUriList.size
    }

    class ImageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var imageView: ImageView = itemView.findViewById<ImageView>(R.id.imageView)
        var delete: ImageView = itemView.findViewById<ImageView>(R.id.delete)
    }

    interface OnItemClickListener {
        fun deleteClick(view: View, position: Int)
        fun itemClick(view: View, position: Int)
    }

}